package com.java110.mall.cart.cmd.refundOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.storeOrder.StoreOrderCartReturnDto;
import com.java110.intf.cart.IStoreOrderCartReturnInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 运营查询 退款订单
 */
@Java110Cmd(serviceCode = "refundOrder.queryAdminRefundOrders")
public class QueryAdminRefundOrdersCmd extends Cmd {

    @Autowired
    private IStoreOrderCartReturnInnerServiceSMO storeOrderCartReturnInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        super.validateAdmin(context);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        StoreOrderCartReturnDto storeOrderCartReturnDto = BeanConvertUtil.covertBean(reqJson, StoreOrderCartReturnDto.class);

        int count = storeOrderCartReturnInnerServiceSMOImpl.queryStoreOrderCartReturnsCount(storeOrderCartReturnDto);

        List<StoreOrderCartReturnDto> storeOrderCartReturnDtos = null;
        if (count > 0) {
            storeOrderCartReturnDtos = storeOrderCartReturnInnerServiceSMOImpl.queryStoreOrderCartReturns(storeOrderCartReturnDto);
            freshShopName(storeOrderCartReturnDtos);
        } else {
            storeOrderCartReturnDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderCartReturnDto.getRow()), count, storeOrderCartReturnDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);

    }
    private void freshShopName(List<StoreOrderCartReturnDto> storeOrderCartReturnDtos) {
        if (ListUtil.isNull(storeOrderCartReturnDtos)) {
            return;
        }
        List<String> shopIds = new ArrayList<>();

        for (StoreOrderCartReturnDto tmpStoreOrderCartReturnDto : storeOrderCartReturnDtos) {
            shopIds.add(tmpStoreOrderCartReturnDto.getShopId());
        }

        ShopDto shopDto = new ShopDto();
        shopDto.setShopIds(shopIds.toArray(new String[shopIds.size()]));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        if (ListUtil.isNull(shopDtos)) {
            return;
        }
        for (StoreOrderCartReturnDto tmpStoreOrderCartReturnDto : storeOrderCartReturnDtos) {
            for (ShopDto tmpShopDto : shopDtos) {
                if (!tmpShopDto.getShopId().equals(tmpStoreOrderCartReturnDto.getShopId())) {
                    continue;
                }
                tmpStoreOrderCartReturnDto.setShopName(tmpShopDto.getShopName());
            }
        }
    }
}
